#include "exitdialog.h"
#include "ui_exitdialog.h"

#include "leftitemdelegate.h"
#include <hyperspin.h>
#include "mainwindow.h"

#include <QDebug>

#include <Windows.h>

ExitDialog::ExitDialog(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::ExitDialog)
{
  ui->setupUi(this);
  ui->listWidget->setItemDelegate(new LeftItemDelegate);
  setWindowFlags(Qt::FramelessWindowHint);
}

ExitDialog::~ExitDialog()
{
  delete ui;
}

void ExitDialog::key(int k)
{
  switch ((ApplicationKey)k)
  {
  case Ak_Up:
    playSound(hs::WAV_UP);
    MainWindow::moveRow(ui->listWidget,-1);
    break;
  case Ak_Down:
    playSound(hs::WAV_DOWN);
    MainWindow::moveRow(ui->listWidget,1);
    break;
  case Ak_Enter:
    playSound(hs::WAV_ENTER);
    accept();
    break;
  case Ak_Back:
    playSound(hs::WAV_BACK);
    setResult(QDialog::Rejected);
    reject();
    break;
  }
}

void ExitDialog::setInput(InputThread *thread, NativeEventBlocker *eventBlocker)
{
  ui->listWidget->installEventFilter(eventBlocker);
  connect(thread,SIGNAL(key(int)),this,SLOT(key(int)));
}

ExitDialog::Action ExitDialog::action() const
{
  int row = ui->listWidget->currentRow();
  if (row == 2)
    return Cancel;
  if (row == 0)
    return ReturnToDesktop;
  return Shutdown;
}

void ExitDialog::paintEvent(QPaintEvent* ev)
{
  QPainter p(this);
  p.drawPixmap(0,0,_bg);
  QDialog::paintEvent(ev);
}

void ExitDialog::playSound(const QString &url)
{
  PlaySound(url.utf16(), 0, SND_ASYNC);
}
